package com.avic.rong.search.adapter.searchResultFragmentsAdapters;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.goodsSort.bean.showRvBean.GoodsSortSearchAllBzBean;
import com.bumptech.glide.Glide;

import java.util.List;

/**
 * Created by Chenjiaxin on 2019/7/20 12:55
 * E-Mail Address：16601203965@163.com
 * 收缩版adapter
 */
public class SearchArticleShrinkRvAdapter extends RecyclerView.Adapter<SearchArticleShrinkRvAdapter.SearchArticleRvViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    private List<GoodsSortSearchAllBzBean.DataBean> list;
    private static int itemCount = 2;
    private SearchHomePageArticleListener listener;

    public SearchHomePageArticleListener getListener() {
        return listener;
    }

    public void setListener(SearchHomePageArticleListener listener) {
        this.listener = listener;
    }

    public SearchArticleShrinkRvAdapter(Context context, List<GoodsSortSearchAllBzBean.DataBean> list) {
        this.context = context;
        this.list = list;
        this.inflater = LayoutInflater.from(context);
    }

    public void addItemCount(int num) {
        itemCount = num;
    }

    @NonNull
    @Override
    public SearchArticleRvViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.search_result_article_item, viewGroup, false);
        return new SearchArticleRvViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SearchArticleRvViewHolder holder, int i) {
        Glide.with(context).load(list.get(i).getImg()).into(holder.img1);
        holder.title.setText(list.get(i).getTitle());
        holder.baoshe.setText(list.get(i).getTypeName());
        holder.readNum.setText(list.get(i).getHot()+"阅读");
        holder.readTime.setText(list.get(i).getPostTime());
        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.searchArticleClick(i);
            }
        });
    }


    @Override
    public int getItemCount() {
        if (list.size() > 2)
            return itemCount;
        else
            return list.size();
    }

    class SearchArticleRvViewHolder extends RecyclerView.ViewHolder {
        ImageView img1,img2;
        TextView title,baoshe,readNum,readTime;
        LinearLayout linearLayout;
        public SearchArticleRvViewHolder(@NonNull View itemView) {
            super(itemView);
            img1 = itemView.findViewById(R.id.search_Result_Article_Img);
            img2 = itemView.findViewById(R.id.search_Result_Article_Img1);
            title = itemView.findViewById(R.id.search_Result_Article_Title);
            baoshe = itemView.findViewById(R.id.search_Result_Article_BaoShe);
            readNum = itemView.findViewById(R.id.search_Result_Article_ReadNum);
            readTime = itemView.findViewById(R.id.search_Result_Article_ReadTime);
            linearLayout = itemView.findViewById(R.id.search_Result_Article_LinearLayout);
        }
    }
    public interface SearchHomePageArticleListener {
        void searchArticleClick(int index);
    }
}
